package home.projects.recipes.chapfour;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.MessageSource;
import org.springframework.context.MessageSourceAware;

public class DBConnector implements InitializingBean, DisposableBean,
      MessageSourceAware {

   private String dbName;

   public void setDbName(String dbName) {
      this.dbName = dbName;
   }

   public String queryForInfo(String queryId) {
      try {
         Thread.sleep(1000);
      } catch (InterruptedException e) {
         e.printStackTrace();
      }
      return "Info [" + queryId + "]";
   }

   @PostConstruct
   public void init() {
      System.out.println("Connection to database [" + dbName + "]");
   }

   @PreDestroy
   public void destroy() {
      System.out.println("Closing connection to database [" + dbName + "]");
   }

   public void afterPropertiesSet() throws Exception {
      System.out.println("After setting the bean properties...");
   }

   public void initMethod() {
      System.out.println("Connection to database in INIT_METHOD [" + dbName + "]");
   }
   
   public void setMessageSource(MessageSource arg0) {
      System.out.println(arg0);
   }

}
